//
//  ChooseMoneyView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/11.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "ChooseMoneyView.h"

@interface ChooseMoneyView()

@property(nonatomic,strong)NSArray *chArray;

@end

@implementation ChooseMoneyView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setChooseArray:(NSArray *)chooseArray {
    _chooseArray = chooseArray;
    
    //适配
    CGFloat plusCount;
    if (iPhone5) {
        plusCount = 10;
    }else {
        plusCount = 20;
    }
    
    UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(15,plusCount,100,14)];
    Label.text = @"充值面额";
    Label.font = [UIFont systemFontOfSize:14];
    [self addSubview:Label];
    Label.textColor = [UIColor grayColor];
    
    
    CGFloat BtnW = WIDTH/4.5;
    CGFloat BtnMargin = (WIDTH-WIDTH/4.5*3)/3;
    
    for (int i = 0; i < self.chooseArray.count; ++i) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        [button setTitle:[NSString stringWithFormat:@"面值:%@",[self.chooseArray[i] objectForKey:@"price"]] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.frame = CGRectMake(BtnMargin/2+(BtnW+BtnMargin)*i,CGRectGetMaxY(Label.frame)+10,BtnW,BtnW/2);
        button.tag = 1000+i;
        [button addTarget:self action:@selector(moneyPayDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        NSString *string = [NSString stringWithFormat:@"%@元\n售价:%@",[self.chooseArray[i] objectForKey:@"price"],[self.chooseArray[i] objectForKey:@"dynamic_price"]];
        
        NSRange priceRange = [string rangeOfString:[NSString stringWithFormat:@"%@元",[self.chooseArray[i] objectForKey:@"price"]]];
        NSRange dynaRange = [string rangeOfString:[NSString stringWithFormat:@"售价:%@",[self.chooseArray[i] objectForKey:@"dynamic_price"]]];
        
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:string];
        [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:priceRange];
        [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12]range:dynaRange];
        button.titleLabel.numberOfLines = 0;
        [button setAttributedTitle:aString forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        
    
        
        if (iPhone5) {
//           button.frame = CGRectMake(BtnMargin/2+(BtnW+BtnMargin)*i,CGRectGetMaxY(Label.frame)+4,BtnW,BtnW/2 - 5);
//            button.titleLabel.font = [UIFont systemFontOfSize:16];
//            titleLabel.font = [UIFont systemFontOfSize:13];
//            [titleLabel setFrame:CGRectMake(button.x, CGRectGetMaxY(button.frame), button.width, 25)];
        }

        
        if (i==0) {
            [button setBackgroundColor:DEFAULT_COLOR];
            NSString *string = [NSString stringWithFormat:@"%@元\n售价:%@",[self.chooseArray[i] objectForKey:@"price"],[self.chooseArray[i] objectForKey:@"dynamic_price"]];
            
            NSRange priceRange = [string rangeOfString:[NSString stringWithFormat:@"%@元",[self.chooseArray[i] objectForKey:@"price"]]];
            NSRange dynaRange = [string rangeOfString:[NSString stringWithFormat:@"售价:%@",[self.chooseArray[i] objectForKey:@"dynamic_price"]]];
            
            NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:string];
            [aString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:priceRange];
            [aString addAttribute:NSForegroundColorAttributeName value: [UIColor whiteColor] range:dynaRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:priceRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12]range:dynaRange];
            button.titleLabel.numberOfLines = 0;
            [button setAttributedTitle:aString forState:UIControlStateNormal];
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        
        
    }
}

- (void)moneyPayDidClick:(UIButton *)sender
{
//    NSLog(@"%@",self.subviews);
    NSLog(@" sender=%@",sender.titleLabel.text)
    ;
    
    for (UIButton *tempBtn in self.subviews) {
        NSLog(@"tempBtn = %ld",tempBtn.tag);
        
        if (tempBtn.tag > 999) {
            
            NSString *string = [NSString stringWithFormat:@"%@元\n售价:%@",[self.chooseArray[tempBtn.tag - 1000] objectForKey:@"price"],[self.chooseArray[tempBtn.tag - 1000] objectForKey:@"dynamic_price"]];
            
            NSRange priceRange = [string rangeOfString:[NSString stringWithFormat:@"%@元",[self.chooseArray[tempBtn.tag - 1000] objectForKey:@"price"]]];
            NSRange dynaRange = [string rangeOfString:[NSString stringWithFormat:@"售价:%@",[self.chooseArray[tempBtn.tag - 1000] objectForKey:@"dynamic_price"]]];
            NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:string];
            [tempBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [tempBtn setBackgroundColor:[UIColor whiteColor]];
            
            [aString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:priceRange];
            [aString addAttribute:NSForegroundColorAttributeName value: [UIColor blackColor] range:dynaRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:priceRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12]range:dynaRange];
            
            tempBtn.titleLabel.numberOfLines = 0;
            [tempBtn setAttributedTitle:aString forState:UIControlStateNormal];
            tempBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        
        if (tempBtn.tag == sender.tag) {

            NSString *string = [NSString stringWithFormat:@"%@元\n售价:%@",[self.chooseArray[sender.tag - 1000] objectForKey:@"price"],[self.chooseArray[sender.tag - 1000] objectForKey:@"dynamic_price"]];
            
            NSRange priceRange = [string rangeOfString:[NSString stringWithFormat:@"%@元",[self.chooseArray[sender.tag - 1000] objectForKey:@"price"]]];
            NSRange dynaRange = [string rangeOfString:[NSString stringWithFormat:@"售价:%@",[self.chooseArray[sender.tag - 1000] objectForKey:@"dynamic_price"]]];
            NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:string];
            [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [sender setBackgroundColor:DEFAULT_COLOR];
            
            [aString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:priceRange];
            [aString addAttribute:NSForegroundColorAttributeName value: [UIColor whiteColor] range:dynaRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:priceRange];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12]range:dynaRange];
            
            sender.titleLabel.numberOfLines = 0;
            [sender setAttributedTitle:aString forState:UIControlStateNormal];
            sender.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        
        
    }
    
    if ([self.delegate respondsToSelector:@selector(ChooseMoneyViewDidClick:)]) {
        
        [self.delegate ChooseMoneyViewDidClick:sender];
    }
}

@end
